#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "image.h"
#include "util_bmp.h"

int main()
{
    uint32_t bmpWidth, bmpHeight;
    uint8_t* bmpMem = UtilBmpGet("../../resource/test/3d.bmp", &bmpWidth, &bmpHeight, NULL);

    Image image(bmpWidth, bmpHeight, IMAGE_FORMAT_RGB);
    ImageGrid grid(image, 3, 3);

    memcpy(image.Mem().addr, bmpMem, image.Size());
    free(bmpMem);

    // image.Clear(0xFF0000, 0, 0, 160, 120);
    // image.Clear(0x00FF00, 160, 0, 160, 120);
    // image.Clear(0xFFFF00, 0, 120, 160, 120);
    // image.Clear(0x0000FF, 160, 120, 160, 120);

#if 0
    ImageGrid gridPiece(image, 100, 100, 200, 100);
    image.Cover(0, 100, gridPiece.Width(), gridPiece.Height(), gridPiece);
    UtilBmpCreate("./out2.bmp", image.Mem().addr, image.Width(), image.Height(), image.Pb());

    Image* tmpImg = gridPiece.ScreenShot();
    UtilBmpCreate("./out3.bmp", tmpImg->Mem().addr, tmpImg->Width(), tmpImg->Height(), tmpImg->Pb());
    delete tmpImg;
#endif

    char input[32];
    while (1)
    {
        Image* retImg = grid.ScreenShot();
        UtilBmpCreate("./out.bmp", retImg->Mem().addr, retImg->Width(), retImg->Height(), retImg->Pb());
        delete retImg;

        //按键移动窗口
        memset(input, 0, sizeof(input));
        if (fgets(input, sizeof(input), stdin) > 0)
        {
            switch(input[0])
            {
                case 'q': return 0;

                case 'w': grid.RangeMove(0, -grid.Height() / 2); break;
                case 's': grid.RangeMove(0, grid.Height() / 2); break;
                case 'a': grid.RangeMove(-grid.Width() / 2, 0); break;
                case 'd': grid.RangeMove(grid.Width() / 2, 0); break;

                case 'n': grid.GridNext(); break;
                default: break;
            }
        }
    }
    return 0;
}
